// 定时任务
let schedule = require("node-schedule");
let { tool } = require("../common/main");

module.exports = {
  // 启动ip定时器
  timer: (timeStr, callback) => {
    try {
      console.log(`启动定时器:${tool.date()}`);
      let count = 0;
      let index = schedule.scheduleJob(timeStr || `1 * * * * *`, () => {
        typeof callback === "function" ? callback(index) : "";
        // console.log(`timer:${tool.date()} [Count=${++count}]`);
      });
      return index;
    } catch (error) {
      console.error(error);
    }
  },
  close: (sysTimeIndex) => {
    console.log(`关闭定时器:${tool.date()}`);
    return schedule.cancelJob(sysTimeIndex);
  }
};
